libname mylib 'd:\Dropbox\Wall Street Bets (Private)\Data';



*This program contains the information needed to create Table 4 of the paper;
*The program references daily_panel_rfs.
This dataset is constructed in  Daily Panel RFS (Intermediate File) code;


*The key dependent variables include:
0) ret0 (current day return) [*note - when ret0 is the dependent variable need to exclude this variable as a control (i.e., drop abn_ret from the set of controls)]
1) ret1 (one-day ahead return)
2) ret2 (two-day ahead return)
3) ret3 (three-day ahead return)
4) ret4 (four-day ahead return)
5) ret5 (five-day ahead return)
6) week2 [6-10 day ahead return]
7) week3 [11-15 day ahead return]
7) week4 [16-20 day ahead return]
8) week5_12 [21-60 day ahead return]

the code below reports reuslts for one-day ahead return - just change dependent variable accordingly;
*code below reports Panel A (full sample). For panel B (sample that excludes GME and AMC), 
impose the filter "where gme_amc_flag  =0" by removing the "*" in the first part of code; 






*info2 contains the necessary variables for the results in Table 3;
data info;
set mylib.daily_panel_rfs;;
ret0 = abn_ret;
keep ticker date cum_month gme_amc_flag post_gme
net_dd2  NET_DD2_POST NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST
DD_and_SA DD_and_SA_POST DD_and_Non_Research DD_and_Non_Research_post net_dd2_pre
 ln_size ln_bm  abn_ret mom5 mom6_26     
BM_MISSING SIZE_MISSING   news_sentiment lag_sent5 lag_sent6_26 SENTIMENT21 ret1 week2-week12 ret0;
*WHERE GME_AMC_FLAG =0;
run;

*will add certain return horizons;
proc sort data=INFO;
by ticker descending date;
run;

data INFO;
set INFO;
count  +1;
by ticker;
if first.ticker then count =1;
ret2 = lag(ret1);
ret3 = lag2(ret1);
ret4 = lag3(ret1);
*note ret5 no longer means the five day return, just the one-day return on day 5;
ret5 = lag4(ret1);
if count <=1 then ret2 =.;
if count <=2 then ret3 = .;
if count <=3 then ret4 = .;
if count <=4 then ret4 = .;
week5_12 =  sum( of week5-week12);
drop week5-week12;
run;



PROC SORT DATA=info;
BY DATE;
RUN;
PROC STANDARD DATA=info OUT=INFO_FE MEAN=0;
BY DATE;
var  
net_dd2  NET_DD2_POST NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST
DD_and_SA DD_and_SA_POST DD_and_Non_Research DD_and_Non_Research_post net_dd2_pre
 ln_size ln_bm  abn_ret mom5 mom6_26     
BM_MISSING SIZE_MISSING   news_sentiment lag_sent5 lag_sent6_26 SENTIMENT21
ret1 ret2 ret3 ret4 ret5 week2 week3 week4 week5_12 ret0;

RUN;




%let yourdata=INFO_FE;

%let firmid=ticker ;
%let time=cum_month;

%let y=ret1 ;
%let x=  net_dd2  NET_DD2_POST  NON_RESEARCH2 NON_RESEARCH2_POST NET_SA2 NET_SA2_POST ln_size ln_bm  abn_ret mom5 mom6_26       news_sentiment lag_sent5 lag_sent6_26   BM_MISSING SIZE_MISSING;
RUN;




proc surveyreg data=&yourdata;

    cluster &firmid;
    model &y = &x /covb;
    ods output covb=firm;
run;
quit;


*cluster by second dimension (e.g., year);
proc surveyreg data=&yourdata;

    cluster &time;
    model &y = &x /covb;
    ods output covb=year;
run;
quit;

*cluster by intersection of the two dimensions (e.g, firm-year);
proc surveyreg data=&yourdata;
    cluster &firmid &time;
    model &y = &x /covb;
    ods output covb=both;
    ods output parameterestimates=parm;
run;
quit;

*keeps original parameter estimates;
data parm; set parm;
    keep parameter estimate;
run;

*returns a dataset with a scalar for the dimensions of the var/cov matrix. This is needed to extract the square roots of the diagonals later on;
data parm1; set parm;
    n=_n_;
    m=1;
    keep m n;
run;

data parm1; set parm1; by m;
    if last.m;
    keep n;
run;

*uses matrix algebra interface to construct Var-cov matrix and extract the standard errors;
proc iml;
    use both;
    read all var _num_ into Z;
    print Z;
    use firm;
    read all var _num_ into X;
    print X;
    use year;
    read all var _num_ into Y;
    print Y;
    use parm1;
    read all var _num_ into n;
    print n;

    B=X+Y-Z;
    C=I(n);
    D=J(n,1);
    E=C#B;
    F=E*D;
    G=F##.5;

    print B;
    print G;
    create b from G [colname='stderr']; ;
    append from G;
quit;

*creates a dataset called 'results' that contains the parameter estimates, the SE's, and the t-stats;
data results; merge parm B;
    tstat=estimate/stderr;
run;

proc print data=results;
run;




data spec3;
set results;
estimate3 = estimate  ;
obs = _n_;
keep parameter estimate3 obs;
run;
data spec3b;
set results;
estimate3 = tstat ;
obs = _n_ + .5;
keep parameter estimate3 obs;
run;
data spec3;
set spec3 spec3b;
run;
proc sort data=spec3;
by obs;
run;
